using System;
using System.Globalization;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;
using System.Reflection;
using Services;
using Utilities.Menu.UIElements;
using BowMaster.Resources;
using BowMaster.SceneElements;
using Utilities;

namespace BowMaster.Levels
{
    abstract class StaticTargetBase3D : ALevel
    {
        protected Texture2D texture, previewTexture, pointTexture;//, hitBoxesText;
        protected StaticTarget3D target;
        protected Rectangle previewRectangle;
        protected int scores;
        protected int lastScore;
        ArrayList<Rectangle> points;
        protected int objective;
        MyButton current_score, objective_score;
        
        public StaticTargetBase3D(Game game, int index, String Name, int objective)
            : base(game, index, Name)
        {
            this.objective = objective;
        }

        protected override void LoadContent()
        {
            texture = Game.Content.Load<Texture2D>("Textures/statictargetpreview");
            //hitBoxesText = Game.Content.Load<Texture2D>("Textures/firingArea");
            previewTexture = Game.Content.Load<Texture2D>("Textures/statictargetpreview");
            pointTexture = Game.Content.Load<Texture2D>("Textures/point");
            previewRectangle = new Rectangle(bar.X - 200, 16, 150, 150);
            points = new ArrayList<Rectangle>();
            scores = 0;
            lastScore = 0;
            base.LoadContent();
            objective_score = new MyButton(new Rectangle(0, 20, 200, 50), Strings.Objective_Score + objective, textColor, arial14);
            current_score = new MyButton(new Rectangle(0, 60, 200, 50), Strings.Current_Score + scores, textColor, arial14);
            
        }
        public override void DrawLevelElements()
        {
            base.DrawLevelElements();
            target.Draw(spriteBatch, view, projection);
            
            //target.DrawHitBoxes(spriteBatch, view, projection, hitBoxesText);
        }
        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);

            spriteBatch.Draw(previewTexture, previewRectangle, Color.White);
            for (int i = 0; i < points.Count; i++)
                spriteBatch.Draw(pointTexture, points[i], Color.Green);
            current_score.Draw(spriteBatch);
            objective_score.Draw(spriteBatch);

            spriteBatch.End();
            
        }
        public override void Update(GameTime gameTime)
        {
            if (!showHelp)
            {
                var dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

                if (activeArrow != null)
                {
                    lastScore = scores;
                    
                    if (target.isHitted(activeArrow))
                    {
                        var point = target.getLastHitPoint();
                        Rectangle p = new Rectangle((int)(point.X * previewRectangle.Width) + previewRectangle.X - 5, (int)(point.Y * previewRectangle.Height) + previewRectangle.Y - 5, 10, 10);
                        points.Add(p);
                        disabledArrows.Add(activeArrow);
                        activeArrow = null;
                        showGetBackButton = true;
                        var d = 1-Vector2.Distance(Vector2.One / 2, point);
                        scores += (int)(d * 5);
                        if (scores >= objective)
                            LevelWon();
                        current_score.text = Strings.Current_Score + scores;
                        current_score.UpdateTextPos();
                    }

                    
                }
            }
            base.Update(gameTime);
        }
    }
}
